package cn.wolfcode.service.impl;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
@Slf4j
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @Transactional
    public void insert(Employee emp, Long[] roleIds) {
        employeeMapper.insert(emp);
        if (roleIds.length>0 && roleIds!=null) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(emp.getId(),roleId);
            }
        }
    }
    @Transactional
    @Override
    public void update(Employee emp, Long[] roleIds) {
        employeeMapper.update(emp);
        employeeMapper.deleteRelation(emp.getId());
        if (roleIds.length>0 && roleIds!=null) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(emp.getId(),roleId);
            }
        }

    }
    @Transactional
    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
        employeeMapper.deleteRelation(id);
    }
    @Transactional
    @Override
    public Employee selectOne(Long id) {

        return employeeMapper.selectForId(id);
    }
    @Transactional
    @Override
    public List<Employee> selectList() {
        return employeeMapper.selectList();
    }
    @Transactional
    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectForList(qo);
        return new PageInfo(list);
        }

    @Override
    public boolean checkUsername(String username) {
        Employee employee=employeeMapper.selectByUsername(username);
        return employee!=null;
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.selectByUsernameAndPassword(username,password);
    }
}
